package com.web.common.ctrl;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.platform.web.chart.json.JSONArray;
import com.css.sword.platform.web.chart.json.JSONException;
import com.css.sword.platform.web.chart.json.JSONObject;
import com.css.sword.platform.web.controller.BaseDomainCtrl;
import com.css.sword.platform.web.controller.annotations.CTRL;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.css.sword.platform.web.fileupload.intimeup.SwordFileItem;
import com.sun.org.apache.xpath.internal.XPathAPI;
import com.web.common.utils.FileDownloadUtiles;
import com.web.common.utils.XMLAdapter;

@CTRL("FileDownloadCtrl")
public class FileDownloadCtrl extends BaseDomainCtrl {

	private static SwordLogUtils log = SwordLogUtils.getLogger(FileDownloadCtrl.class);
	
	/**
	 * 下载附件调用的Ctrl方法
	 * @param req
	 * @return IResData
	 * 2014年6月17日14:42:20
	 * 
	 */
	public IResData download(IReqData req){
		log.debug("开始进入 FileDownloadCtrl_download 方法。");
		String fjname = (String)req.getAttr("fjname");
		Map<String,String> file = new HashMap<String, String>();
		if(StringUtils.isNotEmpty(fjname)){
			file = req.getForm(fjname);
		}else{
			file = req.getForm("fj");
		}
		try {
			String filePath = (String)file.get("path");
			String fileName = (String)file.get("name");
			this.downLoad(new File(filePath), fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return new SwordRes();
	}
	
	/**
	 * 废弃：删除附件调用的Ctrl方法
	 * @param req
	 * @return IResData
	 * 2014年6月17日20:20:06
	 * 
	 */
	public IResData delete(IReqData req){
		log.debug("开始进入 delete 方法。");
		IResData res = new SwordRes();
		Object object = req.getAttr("fjlist");
		String fjname = (String)req.getAttr("fjname");
		Map<String,String> delFile = new HashMap<String, String>();
		if(StringUtils.isNotEmpty(fjname)){
			delFile = req.getForm(fjname);
		}else{
			delFile = req.getForm("fj");
			fjname = "fj";
		}
		try {
			String filexml = "";
			String filePath = (String)delFile.get("path");
			String fileName = (String)delFile.get("name");
			JSONArray jsonarray = new JSONArray(req.getAttr("fjlist").toString());
			for(int i=0;i<jsonarray.length();i++){
				JSONObject jsonobject = jsonarray.getJSONObject(i);
				JSONObject dataMapbject = jsonobject.getJSONObject("dataMap");
				if(dataMapbject.has("myfjxml")){
					filexml = jsonobject.getJSONObject("dataMap").getString("myfjxml");
				}
			}
			if(StringUtils.isNotEmpty(filexml)){
				Document document = XMLAdapter.loadXML(filexml);
				Element rootElement = document.getDocumentElement();
				Element domainElement = (Element) XPathAPI.selectSingleNode(rootElement, "/Files/File[@RealName='"+fileName+"']");
				if(null != domainElement){
					rootElement.removeChild(domainElement);
				}
				res.addListWithDataName(fjname, FileDownloadUtiles.getfileDownloadMap(XMLAdapter.nodeXML(document)));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * 
	 * 暂时废弃的下载附件方法
	 * 2014年6月17日14:41:42
	 * 
	 */
//	public IResData download(IReqData req) throws SwordBaseCheckedException, IOException{
//		// 获取前台传递过来的type=file2的name值
//		String form = (String) req.getAttr("file2");
//		//获取文件信息
//		Map file2 = req.getForm(form);
//		// 获取文件路径
//		String id = (String)file2.get("id");
//		String name = (String)file2.get("name");
//		String path = (String)file2.get("path");
//		IResData res = new SwordRes();
//		req.setAttr("fileid", id);
//		res = (IResData) SwordServiceUtils.callLocalService("FileDownloadService_getFile", req);
////        String path = DocUtil.getFilePath(filePath);
////        // 获取文件名称
////        String file = (String)file2.get("name");
//		List fileList = (List) res.get("fileList");
//		if(0>=fileList.size()){
//			return new SwordRes();
//		}
//		Map fileMap = (Map) fileList.get(0);
//		String fileid = (String) fileMap.get("fileid");
//		String filepath = (String) fileMap.get("filepath");
//		String fileupload = XFEQXMLFileUtils.getFileServerPath("fileupload");
//		this.downLoad(new File(fileupload+filepath+fileid), new String(name.toString().getBytes("UTF-8")));
//		return new SwordRes();
//	}
}